home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0327.ZIP / CDBUTIL.C < prev    next >
Text File  |  1985-11-28  |  13KB  |  481 lines

  1. #include "\lattice\h\glfstdio.h"        /*greenleaf  header*/
  2. #include "\lattice\h\dbase.h"
  3. #include "\lattice\h\fcntl.h"        /*Lattice Header*/
  4. #include "\lattice\h\ctype.h"        /*Lattice header*/
  5. #include "\lattice\h\disk.h"        /*Greenleaf header file*/
  6. /*modify the above PATHs for your own system*/
  7.  
  8. static char *help[]={
  9. "DBASE UTILITIES PROGRAM version 1.0  Lee Mowatt - 10/85",
  10. " To perform a particular function, the command line or the RUN string",
  11. "takes the following generalized form;",
  12. "        CDBUTIL <func#> <.DBF file> [argument list]",
  13. "               (from DOS)",
  14. "                or",
  15. "        RUN CDBUTIL <func#> <.DBF file> [argument list]",
  16. "            (from inside DBASE III)",
  17. "\n",
  18. "FUNC. #        FUNCTION            ARGUMENT LIST",
  19. "======================================================================",
  20. " 1      Database information       <DBFfile> [NDXfile NDXfile...]",
  21. " 2      Unfiltered record count    <DBFfile>",
  22. " 3      Count to memory variable   <DBFfile>",
  23. " 4      Record listing             <DBFfile> [rec.range]",
  24. " 5      Memo field display         <DBFfile> <rec#> [[+],[-],[-rec#]]",
  25. " 6      memo field searching       <DBFfile> <phrase> [rec. range]",
  26. " 7      memo field from textfile   <DBFfile> <rec#> <textfile> [del]",
  27. " 8      disconnect/connect .DBT    <DBFfile> <0 or 1> ",
  28. "                                              (0=disconnect, 1=connect)",
  29. " 9      Pack a .DBT file           <DBFfile>",
  30. " NOTE: [rec. range] is given in the form start#-end# (i.e. 12-234)",
  31. NULL
  32. };
  33.  
  34. struct commlist
  35.     {
  36.     int argnum;                    /*# of args on commandline excluding  CDBUTIL*/
  37.     char *args[20];                /*in-program argument string pointers*/
  38.     };
  39.  
  40. /*the following defines help in reading the function source code*/
  41. #define    FUNC        files->args[0]
  42. #define    DBFILE        files->args[1]
  43. #define NDXFILE(x)    files->args[x+1]
  44. #define RECNUM        files->args[2]
  45. #define PHRASE        files->args[2]
  46. #define RANGE        files->args[3]
  47. #define SUBMODE        files->args[2]
  48. #define TXT            files->args[3]
  49.  
  50. main(argc,argv)
  51.  
  52. int argc;            /*number of arguments*/
  53. char *argv[];        /*argument string pointers*/
  54.     
  55. {
  56. struct commlist arglist;        /*argument structure*/
  57. int loop,count;                    /*general purpose integer variables*/
  58. int function;                    /*C2DBASE function number*/
  59. int fnum;                        /*DBF file number*/
  60. static struct dbinfo db;        /*DBASE information structure-must be static*/
  61. char *ext=".dbf";                /*DBF file extension*/
  62. char *path;                        /*file path name*/
  63. char *stpchr();                    /*Lattice function*/
  64.  
  65. if  ((argc==1)  || (isdigit(argv[1][0]) == 0))
  66.     {
  67.     for (loop=0;help[loop] != NULL;loop++)
  68.         printf("%s\n",help[loop]);
  69.     exit(0);
  70.     }
  71. for (loop=0;loop<argc-1;loop++)
  72.     arglist.args[loop]=argv[loop+1];
  73. arglist.argnum=argc-1;             /*program name not included in argument list*/
  74.  
  75. path[0]=NULL;
  76. strcat(path,argv[2]);
  77. if (stpchr(argv[2],'.') == NULL)    /*add DBF extension if not present*/
  78.     strcat(path,ext);
  79. fnum=dbheader(path, &db);       /*fill information structure*/
  80. if (db.error != 0)                    /*check for error*/
  81.         {
  82.         switch (db.error)
  83.                 {
  84.                 case 1:
  85.                         printf("\7Filename MUST have .DBF extension\n");
  86.                         exit(1); break;
  87.                 case 2:
  88.                         printf("\7Cannot open %s\n",path);
  89.                         exit(1); break;
  90.                 case 3:
  91.                         printf("\7Not a DBASE .DBF file\n");
  92.                         exit(1); break;
  93.                 case 4:
  94.                         printf("\7Cannot open associated .DBT file.\n");
  95.                         exit(1);break;
  96.                 default:
  97.                         printf("Unknown error code - %d\n",db.error);
  98.                         exit(1); break;
  99.                 }
  100.         }
  101.  
  102. stcd_i(argv[1],&function);        /*convert decimal string to integer*/
  103. switch (function)
  104.     {
  105.     case 1:
  106.         func1(&db,&arglist);break;
  107.     case 2:
  108.         func2(&db,&arglist);break;
  109.     case 3:
  110.         func3(&db,&arglist);break;
  111.     case 4:
  112.         func4(&db,&arglist);break;
  113.     case 5:
  114.         func5(&db,&arglist);break;
  115.     case 6:
  116.         func6(&db,&arglist);break;
  117.     case 7:
  118.         func7(&db,&arglist);break;
  119.     case 8:
  120.         func8(&db,&arglist);break;
  121.     case 9:
  122.         func9(&db,&arglist);break;
  123.     default:
  124.         printf("\7Unknown CDBUTIL function number\n");break;
  125.     }
  126. dbclose(&db);
  127. }
  128.  
  129.  
  130. /**********************************************************************/
  131. func1(dbase,files)            /*display file information*/
  132.  
  133. struct dbinfo *dbase;
  134. struct commlist *files;
  135.  
  136. {
  137. int loop;
  138. char *stpchr();                /*Lattice function*/
  139. char *string,*temp;            /*general purpose caharacter pointer*/
  140. char *path;                    /*pathname*/
  141. char c=0x5c;                /*ascii number for '\'*/
  142. char *ext=".ndx";            /*index extensionto be appended if not present*/
  143.  
  144. for (loop=3;loop<=files->argnum;loop++)            /*process index files*/
  145.     {
  146.     if ((string=stpchr(DBFILE,c)) != NULL)    /*PATH processing*/
  147.         {
  148.          while ((temp=stpchr(string,c)) != NULL)
  149.             string=temp+1;                     /*find last '\'*/
  150.         stccpy(path,DBFILE,(string-DBFILE+1));
  151.         }
  152.     strcat(path,NDXFILE(loop-2));
  153.     if (stpchr(path,'.') == NULL)
  154.         strcat(path,ext);
  155.     ndxread(dbase,path);
  156.     /*test for errors*/
  157.     if (dbase->error !=0)
  158.         {
  159.         switch (dbase->error)
  160.             {
  161.             case 1:
  162.                 printf("\7Index file must have .NDX extension\n");
  163.                 exit(1);break;
  164.             case 2:
  165.                 printf("\7Cannot open %s\n",path);
  166.                 exit(1);break;
  167.             case 3:
  168.                 printf("\7Wrong index file for database being used\n");
  169.                 exit(1);break;
  170.             default:
  171.                 printf("\7Unknown ndxread error code-%d\n",dbase->error);
  172.                 exit(1);break;
  173.             }
  174.         }
  175.     }
  176. dbstat(dbase);            /*display files information*/
  177. }
  178.  
  179.  
  180. /**********************************************************************/
  181. func2(dbase,files)                    /*unfiltered record count to screen*/
  182.  
  183. struct dbinfo *dbase;
  184. struct commlist *files;
  185.  
  186. {
  187. printf("%ld records in %s\n",dbase->info.quan,dbase->filname);
  188. }
  189.  
  190.  
  191. /**********************************************************************/
  192. func3(dbase,files)
  193.  
  194. struct dbinfo *dbase;
  195. struct commlist *files;
  196.  
  197. {
  198. char count[10];                        /*string of record count*/
  199. char c=0x5c;                        /*ascii number for '\'*/
  200. char *string,*temp;                    /*general purpose string variables*/
  201. char path[35];                            /*directory to write .MEM file*/
  202. char *stpchr();                        /*Lattice function*/
  203.  
  204. stci_d(count,(int)(dbase->info.quan),10);    /*convert record number to string*/
  205.  
  206. if ((string=stpchr(DBFILE,c)) != NULL)    /*PATH processing*/
  207.     {
  208.     while ((temp=stpchr(string,c)) != NULL)
  209.         string=temp+1;                     /*find last '\'*/
  210.     stccpy(path,DBFILE,(string-DBFILE));
  211.     }
  212.  
  213. pass(count,path);
  214. }
  215.  
  216.  
  217. /**********************************************************************/
  218. func4(dbase,files)                /* record listing*/
  219.  
  220. struct dbinfo *dbase;
  221. struct commlist *files;
  222.  
  223. {
  224. char *range;
  225. long start,end,strtol();
  226. char *string,*record,*stpchr();
  227. char *temp;
  228.  
  229. if ((files->argnum < 3) || (isdigit(files->args[2][0]) == 0))  
  230.     {start=1L;    end=dbase->info.quan;}    /*no range specified,do all records*/
  231.   else
  232.     {
  233.     range=files->args[2];
  234.     string=stpchr(range,'-');
  235.     strxlf(record,range,(string-range));
  236.     start=strtol(record,&temp,10);
  237.     strxrt(record,range,strlen(range)-(string-range+1));
  238.     end=strtol(record,&temp,10);
  239.     }
  240.  
  241. reclist(dbase,start,end);
  242. }
  243.  
  244.  
  245. /**********************************************************************/
  246. func5(dbase,files)
  247.  
  248. struct dbinfo *dbase;
  249. struct commlist *files;
  250. {
  251. char *memoread();                /*C2DBASE function*/
  252. char *memotext;                    /*text pointer*/
  253. char *memoname;                    /*name of memo field*/
  254. char *stpchr();                    /*Lattice function*/
  255. long rec,start,end;                /*record numbers*/
  256. int fieldspace;                    /*length of memo text*/
  257. int count;                        /*general purpose integer variable*/
  258. long strtol();                    /*Lattice function*/
  259. char *temp,*record;
  260.  
  261. for (count=0; count<dbase->fieldnum; count++)  /*check that there is a memo field*/
  262.         {
  263.         if (dbase->field[count].type == 'M')
  264.                 memoname=dbase->field[count].name;
  265.         }
  266. memoread(dbase,1L);              /*see if this function returns an error*/
  267. if ( (dbase->error == 1) || (dbase->error == 2) ) /*no memo fields to be read*/
  268.         {
  269.         printf("No memo field in this database\n");
  270.         exit(1);
  271.         }
  272. if (stpchr(RECNUM,'+') !=NULL)
  273.     {
  274.     start=strtol(RECNUM,&memotext,10);        /*record specified*/
  275.     end=dbase->info.quan;
  276.     }
  277.   else
  278.     {
  279.     if ((temp=stpchr(RECNUM,'-')) != NULL)
  280.         {
  281.         if (strlen(RECNUM) > (temp-RECNUM+1))                /*range specified*/
  282.             {
  283.             strxlf(record,RECNUM,(RECNUM - temp));
  284.             start=strtol(record,&memotext,10);
  285.             strxrt(record,RECNUM,strlen(RECNUM)-(temp-RECNUM+1));
  286.             end=strtol(record,&memotext,10);
  287.             }
  288.           else
  289.             {
  290.             start=0L;
  291.             end=strtol(RECNUM,&memotext,10);
  292.             }
  293.         }
  294.       else
  295.         {
  296.         start=strtol(RECNUM,&memotext,10);
  297.         end=start;
  298.         }
  299.     }
  300. for (rec=start;rec<=end;rec++)
  301.     {
  302.     memotext=memoread(dbase,rec);
  303.     if ((dbase->error == 0) || (dbase->error == 5))
  304.         {
  305.         printf("\n***%s for record %ld***\n",memoname,rec);
  306.         fieldspace=strlen(memotext);
  307.         for (count=0;count<fieldspace;count++)
  308.             bdos(6,memotext[count]);
  309.         printf("\n");
  310.         if (dbase->error == 5)
  311.             printf("Display truncated.Memo text longer than 2048 characters\n");
  312.         }
  313.       else
  314.         {
  315.         switch (dbase->error)
  316.             {
  317.             case 3:
  318.                 printf("no memo text for record %ld\n",rec);break;
  319.             case 4:
  320.                 printf("error reading .DBT file\n");break;
  321.             default:
  322.                 printf("Unknown memoread error -#%d\n",dbase->error);
  323.                 break;
  324.             }
  325.         }
  326.     }
  327. }
  328.  
  329.  
  330. /**********************************************************************/
  331. func6(dbase,files)
  332.  
  333. struct dbinfo *dbase;
  334. struct commlist *files;
  335. {
  336. char *memoread();
  337. long rec,start,end;                /*record numbers*/
  338. char *temp,*string,*record;        /*general purpose string variable*/
  339. char *stpchr();
  340. long strtol();                    /*Lattice function*/
  341. int find,found=FALSE;            /*find status of textfind function*/
  342. int loop,count;
  343.  
  344. memoread(dbase,1L);              /*see if this function returns an error*/
  345. if ( (dbase->error == 1) || (dbase->error == 2) ) /*no memo fields to be read*/
  346.         {
  347.         printf("No memo field in this database\n");
  348.         exit(1);
  349.         }
  350.  
  351. count=strlen(PHRASE);
  352. for (loop=0;loop<count;loop++)
  353.     {
  354.     if (PHRASE[loop] == '_')
  355.         PHRASE[loop] = 0x20;
  356.     }
  357.  
  358. if ((files->argnum < 4) || (isdigit(RANGE[0]) == 0))     /*no range specified*/
  359.     {start=1L;    end=dbase->info.quan;}
  360.   else
  361.     {                                                    /*range given*/
  362.     string=stpchr(RANGE,'-');
  363.     strxlf(record,RANGE,(string-RANGE));
  364.     start=strtol(record,&temp,10);
  365.     strxrt(record,RANGE,strlen(RANGE)-(string-RANGE+1));
  366.     end=strtol(record,&temp,10);
  367.     }
  368.  
  369. printf("search for %s\n",PHRASE);
  370. for (rec=start;rec<=end;rec++)
  371.     {
  372.     find=textfind(dbase,rec,PHRASE);
  373.     switch (find)
  374.         {
  375.         case 1:
  376.             found=TRUE;
  377.             printf("\nRecord %ld ",rec);
  378.             break;
  379.         case 0:
  380.             printf(".");break;
  381.         case -1:
  382.             if (dbase->error == 2)
  383.                 {
  384.                 printf("Error- search string is NULL\n");
  385.                 exit(1);
  386.                 }
  387.               else
  388.                 {
  389.                 if (dbase->error == 3)
  390.                     printf("Error reading .DBT file.\n");
  391.                 }
  392.             break;
  393.         default:
  394.             printf("Unknown error\n"); break;
  395.         }
  396.     }
  397. printf("\n");
  398. }
  399.  
  400.  
  401. /**********************************************************************/
  402. func7(dbase,files)
  403.  
  404. struct dbinfo *dbase;
  405. struct commlist *files;
  406. {
  407. unsigned memoadd();                /*C2DBASE function*/
  408. static long rec;                /*record number*/
  409. long strtol();                    /*Lattice function*/
  410. char *stpchr();                    /*Lattice function*/
  411. struct DISKTABLE text;            /*structure for Greenleaf function 'dos2delete*/
  412.                                 /*found in disk.h header*/
  413.  
  414. rec=strtol(RECNUM);
  415. if (stpchr(TXT,'.') ==NULL)        /* test for proper arguments*/
  416.     {printf("\7Command line error\n");    exit(1);}
  417.  
  418. if (memoadd(dbase,rec,TXT) == -1)
  419.     {
  420.     switch (dbase->error)
  421.         {
  422.         case 0:
  423.             break;
  424.         case 1:
  425.             printf("\7Record does not have a memo field.\n");    break;
  426.         case 2:
  427.             printf("\7Cannot open %s\n",TXT);    break;
  428.         case 3:
  429.             printf("\7Cannot re-open .DBT file\n");    break;
  430.         default:
  431.             printf("\7Unknown error\n");    break;
  432.         }
  433.     }
  434.   else
  435.     {
  436.     if ((files->args[4] == "del") || (files->args[4]=="DEL"))
  437.         {
  438.         text.string=TXT;
  439.         dos2delete(&text);            /*delete text file*/
  440.         }
  441.     }
  442. }
  443.  
  444.  
  445. /**********************************************************************/
  446. func8(dbase,files)
  447.  
  448. struct dbinfo *dbase;
  449. struct commlist *files;
  450. {
  451. onoffdbt(dbase,SUBMODE);
  452. }
  453.  
  454.  
  455. /**********************************************************************/
  456. func9(dbase,files)
  457.  
  458. struct dbinfo *dbase;
  459. struct commlist *files;
  460. {
  461. if (dbtpack(dbase) != 0)
  462.     {
  463.     switch (dbase->error)
  464.         {
  465.         case 1:
  466.             printf("\7Database has no .DBT file\n");    break;
  467.         case 2:
  468.             printf("\7Cannot open original .DBT file\n");    break;
  469.         case 3:
  470.             printf("\7Cannot create new .DBT file\n");    break;
  471.         case 4:
  472.             Printf("\7Error writing to new .DBT file\n");    break;
  473.         case 5:
  474.             printf("\7Error updating .DBF file\n");        break;
  475.         default:
  476.             printf("\7Unknown DBTPACK error code\n");    break;
  477.         }
  478.     }
  479. }
  480.             
  481.